#include<stdio.h>
#include<assert.h>
//求字符串长度
//size_t strlen(const char* str)
//{
//  assert(str);
//  const char* end = str;
//  while(*end) end++;
//  return end - start;
//}
//size_t strlen(const char* str)
//{
//  assert(str);
//  size_t count = 0;
//  while(*str)
//  {
//    count++;
//    str++;
//  }
//  return count;
//}
size_t strlen(const char* str)
{
  assert(str);
  if(*str)
    return 1+ strlen(str+1);
  else 
    return 0;
}

//字符串拷贝
char* strcpy(char* dest,const char* str)
{
  assert(dest && str);
  char* ret = dest;
  while(*dest++ = *str++) ;
  return ret;
}

//字符串追加
char* strcat(char* dest,const char* str)
{
  assert(dest && str);
  char* ret = dest;
  while(*dest) dest++;
  while(*dest++ = *str++);
  return ret;
}

//字符串比较
int strcmp(const char* dest,const char* str)
{
  assert(dest && str);
  while((*dest == *str) && *dest && *str)
  {
    dest++,str++;
  }
  return *dest - *str;
}

//找子串
char* strstr(const char* str1,const char* str2)
{
  const char* start = str1;
  const char* dest = NULL,*str = NULL;
  if(strlen(str2) > strlen(str1) || str1 == NULL)
    return NULL;
  if(strcmp(str1,str2) == 0 || str2 == NULL) return (char*)str1;
  while(*start)
  {
    dest = start;
    str = str2;
    while((*dest == *str) && *dest && *str)
    {
      dest++,str++;
    }
    if(*str == '\0') return (char*)start;
    
    start++; //从下一个位置开始匹配
  }
  return NULL;
}

//长度受限的函数,共比较count个字符
int strncmp(const char* dest,const char* str,size_t count)
{
  assert(dest && str);
  while(count-- && *dest && *str && (*dest == *str)) dest++,str++;
  return *dest - *str;
}

char* strncat(char* dest,const char* str,size_t count)
{
  assert(dest && str);
  char* ret = dest;
  while(*dest++);
  while(count-- && (*dest++ = *str++)) ;
  *dest = '\0';
  return ret;
}

char* strncpy(char* dest,const char* str,size_t count)
{
  assert(dest && str);
  char* ret = dest;
  while(count-- && (*dest++ = *str++)) ;
  while(count--)
    *dest++ = '\0';
  return ret;
}

//内存函数系列
void* memcpy(void* dest,const void* str,size_t count) //拷贝多少字节
{
  assert(dest && str);
  void* ret = dest;
  while(count--)
  {
    *(char*)dest = *(char*)str;
    dest = (char*)dest+1; //不能写成(char*)dest++ 因为有些编译器不支持
    str = (char*)str+1;
  }
  return ret;
}

int memcmp(const void* dest,const void* str,size_t count)
{
  assert(dest&&str);
  while(count--)
  {
    if(*(char*)dest == *(char*)str)
    {
      dest = (char*)dest+1;
      str = (char*)str+1;
    }
    else 
      return *(char*)dest - *(char*)str; //字节相减
  }
  return 0;
}


//解决内存重叠问题
void* memmove(void* dest,const void* str,size_t count)
{
  assert(dest&&str);
  void* ret = dest;
  //[........]...........]
  //[.....[......].......]
  if(dest < str)
  {
    while(count--)
    {
      *(char*)dest = *(char*)str;//从前往后拷
      dest = (char*)dest+1;
      str = (char*)str+1;
    }
  }
  else  //从后往前拷
  {
    while(count--)
    {
      *((char*)dest + count) = *((char*)str + count);
    }
  }
  return ret;
}

void* memset(void* dest,int c,size_t count)
{
  void* ret = dest;
  char set_c = (char)c;
  while(count--)
  {
    *(char*)dest = set_c;
    dest = (char*)dest +1;
  }
  return ret;
}
int main()
{
  char s[50];
  const char* str = "Mango Mango";
  printf("%s\n",strcpy(s,str));
  return 0;  
}
